// userController.js

const express = require('express');
const router = express.Router();
const db = require('./db');

// 获取所有用户
router.get('/', async (req, res) => {
    try {
        const users = await db.query('SELECT * FROM User');
        res.json(users);
    } catch (err) {
        console.error(err);
        res.status(500).send('Server Error');
    }
});

// 根据用户ID获取用户信息
router.get('/:id', async (req, res) => {
    const userId = req.params.id;
    try {
        const user = await db.query('SELECT * FROM User WHERE id = ?', [userId]);
        if (user.length > 0) {
            res.json(user[0]);
        } else {
            res.status(404).send('User not found');
        }
    } catch (err) {
        console.error(err);
        res.status(500).send('Server Error');
    }
});

module.exports = router;